package com.deyuanyun.pic.ae.controller;

import com.deyuanyun.pic.ae.controller.dto.*;
import com.deyuanyun.pic.ae.controller.vo.PressObjectAuditVO;
import com.deyuanyun.pic.ae.controller.vo.PressObjectVO;
import com.deyuanyun.pic.ae.domain.PressObject;
import com.deyuanyun.pic.ae.enumeration.AEExportEnum;
import com.deyuanyun.pic.ae.enumeration.FindTypeEnum;
import com.deyuanyun.pic.ae.service.PressObjectService;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “管道占压物” 管理控制类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-12
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/ae/pressObject")
public class PressObjectController extends AEBaseController {

    @Resource
    private PressObjectService pressObjectService;

    /**
     * 跳转到 “管道占压物” 管理页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/index.html")
    public ModelAndView index(ModelMap model){
        return forword("/ae/pressObject", model);
    }

    /**
     * 条件 查询“管道占压物”
     *
     * @param aeBaseQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(AEBaseQuery aeBaseQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryVO(aeBaseQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 条件 导出“管道占压物”
     *
     * @param aeBaseQuery
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(AEBaseQuery aeBaseQuery) throws IOException {
        List list = this.queryVO(aeBaseQuery);
        AEExportEnum exportEnum = AEExportEnum.PressObject;
        String[] columnKeys = {"pipeLineName","name","type","contact","telephone",
                "nagmInput","nagmMileageInput","locationDescription","minDistance","coverDepth",
                "foundDate", "foundWay", "recorder", "recordTime", "remarks"};
        DownExcelUtil.export(exportEnum.getFileName(), aeTemplatePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    /**
     * 根据id查询 “管道占压物”
     *
     * @param id
     */
    @RequestMapping("/find.json")
    @ResponseBody
    public void find(String id, FindTypeEnum type) throws IOException {

        Asserts.assertLogic(StringUtils.isNullEmpty(id), "管道占压物id 不能为空");
        PressObject pressObject = pressObjectService.find(id);
        if (FindTypeEnum.edit.equals(type)) {
            PressObjectEdit pressObjectEdit = new PressObjectEdit();
            pressObjectEdit.fromDomain(pressObject);
            AjaxSupport.sendSuccessText("", pressObjectEdit);
        }
        if (FindTypeEnum.show.equals(type)) {
            PressObjectAuditVO pressObjectVO = new PressObjectAuditVO();
            pressObjectVO.fromDomain(pressObject);
            AjaxSupport.sendSuccessText("", pressObjectVO);
        }
        AjaxSupport.sendSuccessText("", "类型错误，没有任何数据");
    }

    /**
     * 创建 或者 编辑“管道占压物”
     *
     * @param pressObjectEdit
     */
    @RequestMapping("/edit.json")
    @ResponseBody
    public void edit(PressObjectEdit pressObjectEdit) {
        int updateRows = pressObjectService.edit(pressObjectEdit.toDomain(), pressObjectEdit.getImgs());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 审核“管道占压物”
     *
     * @param aeBaseAudit
     */
    @RequestMapping("/audit.json")
    @ResponseBody
    public void audit(AEBaseAudit aeBaseAudit) {
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getId()), "管道占压物id 不能为空");
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getDataStated()), "审核状态 不能为空");
        int updateRows = pressObjectService.audit(aeBaseAudit.toPressObject());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 删除“管道占压物”
     *
     * @param id
     */
    @RequestMapping("/delete.json")
    @ResponseBody
    public void delete(String id) {
        int updateRows = pressObjectService.delete(id);
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    private Page<PressObjectVO> queryVO(AEBaseQuery aeBaseQuery) {
        return new PressObjectVO().fromDomainList(pressObjectService.query(aeBaseQuery));
    }
}
